package com.dwxt.parabolic.utils;

import com.dwxt.common.util.JwtUtils;
import com.dwxt.common.util.PartyUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

@Component
public class RequestUtil {
    private static HttpServletRequest request;

    private static HttpServletResponse response;

    //private static JwtUtil jwtUtil;

    public static HttpServletResponse getResponse() {
        return response;
    }

	/*@Autowired
	public void setResponse(HttpServletResponse response) {
		PartyUtil.response = response;
	}*/

    public static HttpServletRequest getRequest() {
        return request;
    }

    @Autowired
    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    //获取当前userInfo
    public static String getCurrentUserId() {
        // 从header里获取token
        String authToken = request.getHeader("Authorization");
        //System.out.println(authToken);
        if (StringUtils.isNotBlank(authToken)) {
            String res = RedisUtil.get(authToken, String.class);
            return res.split(",")[1];
        }
        return null;
    }

    //获取当前公司
    public static String getCurrentCompanyId() {
        // 从header里获取token
        String authToken = request.getHeader("Authorization");
        //System.out.println(authToken);
        if (StringUtils.isNotBlank(authToken)) {
            String res = RedisUtil.get(authToken, String.class);
            return res.split(",")[0];
        }
        return null;
    }

    //获取当前用户角色
    public static String getCurrentUserType() {
        // 从header里获取token
        String authToken = request.getHeader("Authorization");
        //System.out.println(authToken);
        if (StringUtils.isNotBlank(authToken)) {
            String res = RedisUtil.get(authToken, String.class);
            return res.split(",")[2];
        }
        return null;
    }

}
